package com.oil.petro.eshop.controller;

import com.oil.petro.eshop.dao.AccountDAO;

import com.oil.petro.eshop.entity.Account;
import com.oil.petro.eshop.entity.Person;
import com.oil.petro.eshop.service.UserAccountService;
import com.oil.petro.eshop.service.UserAccountServiceImpl;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller

public class MainController {

    @Autowired
    UserAccountService userAccountService;
    
    @RequestMapping("/main")
    public ModelAndView renderPage() {
        ModelAndView mav = new ModelAndView();
        System.out.println("AAAAAAAAAAAA");
        mav.setViewName("mainView");

//    System.out.println("AAAAAAAAAAAA");
////        der.applySettings(cfg.getProperties());
////        ServiceRegistry sr = builder.buildServiceRegistry();
//        cfg.buildSessionFactory(sr);ServiceRegistryBuilder builder = new ServiceRegistryBuilder();
//        builder.applySettings(cfg.getProperties());
//        ServiceRegistry sr = builder.buildServiceRegistry();
//        cfg.buildSessionFactory(sr);
//            Person ME = new Person();
//    UserDAO dao = new UserDAO();
//            /* Add few employee records in database */
//            dao.addEmployee("Zara", "Ali", "Kutno");ave is not valid
//            
//            UserDAO user = new UserDAO();
        //   Person person = new Person("Marcin", "Cichonski", "99-300","Gandhiego", "Łódź");
//        
//        int id = userAccountService.savePerson(person);
//        System.out.println("Numer person" +id);
//        Person p2 = userAccountService.getPerson(id);
//        
//        System.out.println("uzytkownik p2"+p2.getAddress() );
//        
//        p2.setAddress("Kosciuszki");
//        userAccountService.updatePerson(p2);
//       Person  p3 = userAccountService.getPerson(id);
//        
//        System.out.println("uzytkownik p3"+p3.getAddress() );
//        userAccountService.deletePerson(person);
//        
        Person person = new Person("Marcin", "Cichonski", "99-300", "Gandhiego", "Łódź");
        Account account = new Account("login4", "password4", "email4", 1);

//          int id2 = userAccountService.saveAccount(account);
//        System.out.println("Numer account" +id2);
//        Account a2 = userAccountService.getAccount(id2);
//        
//        System.out.println("konto a2"+a2.getEmail() );
//        
//        a2.setEmail("emailKosciuszki");
//        userAccountService.updateAccount(a2);
//       Account  a3 = userAccountService.getAccount(id2);
//        
//        System.out.println("account a3"+a3.getEmail() );
//        userAccountService.deleteAccount(account);
//            Account account = new Account("login4","password4","email4",1);
//           // AccountDAO daoA = new AccountDAO();
//            //daoA.add(account);
//            uaccount.addAccount(account);
//            
//            System.out.println(uaccount.getAccount(5).getEmail());
//            UserAccountService accountService= new UserAccountService();
//           Integer id = accountService.registerUserAccouny(account);
//            
//            account =  accountService.getUserAccount(id);
//            account.setEmail("testemail");
//           //Account account = new Account("login3","password","email",1);
//           accountService.updateUserAccount(account);
//          // accountService.deleteUserAccount(account);
//        Test t = new Test();
//        t.setName("test");
        //AbsenceDAO dao = new AbsenceDAO();
        //dao.addAbsence(u);
        //mav.addObject("p", "e");
        return mav;
    }

    @RequestMapping("/userInfo")
    public ModelAndView getUserDetails() {
        Person person = new Person("Marcin", "Cichonski", "99-300", "Gandhiego", "Łódź");
        Account account = new Account("login4", "password4", "email4", 1);
       
        Map<String, Object> model = new HashMap<String, Object>();
         model.put("person", person);
         model.put("account", account);
//        model.put("email", account.getEmail());
//         model.put("login", account.getLogin());
//          model.put("password", account.getPassword());
//        model.put("firstname", person.getFirstName());
//         model.put("lastname", person.getLastName());
//          model.put("city", person.getCity());
//            model.put("address", person.getAddress());
//            model.put("postcode", person.getPostCode());
        return new ModelAndView("userDetails", model);

    }
    
    
}
